﻿using System;
using System.Data.Common;
using System.Linq;
using AChance.Dal.Entity;
using AChance.Func;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace AChance.Dal.Func
{
    public class DalSqlContext : DbContext
    {
        #region 基础配置
        private readonly DalSqlBase.Type _sqlType;
        private readonly string _connectString;
        public DalSqlContext(DalSqlBase.Type sqlType, string connectString)
        {
            _sqlType = sqlType;
            _connectString = connectString;
            ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
            ChangeTracker.AutoDetectChangesEnabled = false;
            ChangeTracker.LazyLoadingEnabled = false;
            Database.SetCommandTimeout(1000);
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            switch (_sqlType)
            {
                case DalSqlBase.Type.Dm:
                    optionsBuilder.UseDm(_connectString).AddInterceptors(new SqlCommandInterceptor());
                    break;
                default:
                    optionsBuilder.UseSqlServer(_connectString).AddInterceptors(new SqlCommandInterceptor());
                    break;
            }
        }
        public class SqlCommandInterceptor : DbCommandInterceptor
        {
            public override void CommandFailed(DbCommand command, CommandErrorEventData eventData)
            {
                FunLogs.LogErrorForSqlExec(eventData, command.CommandText.ToRawSql(command.Parameters));
            }
            public override DbDataReader ReaderExecuted(DbCommand command, CommandExecutedEventData eventData, DbDataReader result)
            {
                TimeSpan execTime = DateTime.Now - eventData.StartTime.LocalDateTime;
                if (execTime.TotalMilliseconds > 3000)
                    FunLogs.LogWarnForSqlExec(eventData, command.CommandText.ToRawSql(command.Parameters), execTime);
                return result;
            }
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder
                .Entity<FDWORG>(build =>
                {
                    build.HasKey(entity => entity.ORGID);
                    build.Ignore(entity => entity.EXTORGABC);
                    build.Ignore(entity => entity.BDRID);
                })
                .Entity<FDWUDT>(build =>
                {
                    build.HasKey(entity => entity.UDTTABLENAME);
                })
                .Entity<FDWSDD>(build =>
                {
                    build.HasKey(entity => new { entity.SDDORGID, entity.SDDNAME, entity.SDDVALUE });
                })
                .Entity<FDWBDR>(build =>
                {
                    build.HasKey(entity => entity.BDRID);
                });

            modelBuilder.Model.GetEntityTypes().ToList().ForEach(entityType =>
                entityType.GetProperties().ToList().ForEach(property =>
                {
                    modelBuilder.Entity(entityType.Name).Property(property.Name).IsRequired();
                })
            );
        }
        #endregion

        #region 系统业务表
        public DbSet<FDWORG> FDWORG { get; set; }
        public DbSet<FDWUDT> FDWUDT { get; set; }
        //public DbSet<FDWUPD> FDWUPD { get; set; }
        public DbSet<FDWSDD> FDWSDD { get; set; }
        //public DbSet<FDWACT> FDWACT { get; set; }
        #endregion

        #region 其它表


        #endregion

    }
}
